package com.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.pagehelper.PageInfo;
import com.pinda.common.CustomIdGenerator;
import com.pinda.commonutils.Page;
import com.pinda.commonutils.Result;
import com.pinda.commonutils.StatusCode;
import com.pinda.feign.PdCoreOrgFeignClient;
import com.pinda.feign.PdDriverJobFeignClient;
import com.pinda.mapper.PdDriverJobMapper;
import com.pinda.vo.HistoryVo;
import com.pinda.work.entity.PdDriverJob;

import com.pinda.service.IPdDriverJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pinda.work.enums.driverjob.DriverJobStatus;
import com.pinda.work.vo.Pddriverjob_TasktransportView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;




/**
 * <p>
 * 司机作业单 服务实现类
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@Service
public class PdDriverJobServiceImpl extends ServiceImpl<PdDriverJobMapper, PdDriverJob> implements IPdDriverJobService {

    @Resource
    private PdDriverJobMapper pdDriverJobMapper;

    @Autowired
    private PdDriverJobFeignClient pdDriverJobFeignClient;

    @Autowired
    private CustomIdGenerator idGenerator;


    @Override
    public PdDriverJob saveDriverJob(PdDriverJob driverJob) {
        driverJob.setId(idGenerator.nextId(driverJob) + "");
        driverJob.setCreateTime(String.valueOf(LocalDateTime.now()));
        driverJob.setStatus(DriverJobStatus.PENDING.getCode());
        save(driverJob);
        return driverJob;
    }


    @Resource
    private PdCoreOrgFeignClient pdCoreOrgFeignClient;
    //获取待提货、历史记录
    @Override
    public Result<List<PdDriverJob>> getDriverJobByDriverIdAndTaskId(String driverId,Integer status) {
        QueryWrapper<PdDriverJob> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",driverId);
        queryWrapper.eq("status",status);
        List<PdDriverJob> list = pdDriverJobMapper.selectList(queryWrapper);
        if(list!=null){
            return new Result<>(true, StatusCode.OK, "成功", list);
        }
        return new Result<>(false, StatusCode.ERROR,"失败",null);
    }
    //判断该司机是否有在途任务
    @Override
    public boolean extract(String driverId,Integer status) {
        QueryWrapper<PdDriverJob> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        queryWrapper.eq("driver_id",driverId);
        return pdDriverJobMapper.exists(queryWrapper);
    }
//修改司机作业单
    @Override
    public boolean upDriverJob(String driverId, PdDriverJob pdDriverJob) {
        QueryWrapper<PdDriverJob> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",driverId);
        queryWrapper.eq("task_transport_id",pdDriverJob.getTaskTransportId());
        int i = pdDriverJobMapper.update(pdDriverJob,queryWrapper);
        return i!=0;
    }

    @Override
    public PdDriverJob selectOne(Integer status, String driverId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",status);
        queryWrapper.eq("driver_id",driverId);
        PdDriverJob pdDriverJob = pdDriverJobMapper.selectOne(queryWrapper);
        return pdDriverJob;
    }

    @Override
    public Result<HistoryVo> selectOnePdDriverJob(String taskTransportId) {
        HistoryVo historyVo = this.pdDriverJobMapper.selectHistoryVo(taskTransportId);
        historyVo.setStartAgency(this.pdCoreOrgFeignClient.selectName(historyVo.getStartAgencyId()));
        historyVo.setEndAgency(this.pdCoreOrgFeignClient.selectName(historyVo.getEndAgencyId()));
        System.out.println(historyVo.toString());
        if (historyVo!=null){
            return new Result<>(true,200,"成功",historyVo);
        }
        return new Result<>(false,300,"失败",null);
    }

    /**
     * 分页查询司机作业单表聚合数据库远程调用
     * @param page
     * @return
     */
    @Override
    public PageInfo<Pddriverjob_TasktransportView> PdddriverjobTasktransportService(Page<Pddriverjob_TasktransportView> page) {
        PageInfo<Pddriverjob_TasktransportView> pddriverjobTasktransportViews = this.pdDriverJobFeignClient.querypdDriverjobController(page);
        return pddriverjobTasktransportViews;
    }
}
